package com.loiane.dao;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.loiane.com.model.City;
import com.loiane.util.HibernateUtil;

/**
 * City DAO
 * Interacts with database.
 * Implemented for academic purposes.
 * 
 * @author Loiane Groner
 * http://loianegroner.com (English)
 * http://loiane.com (Portuguese)
 */
public class CityDAO extends BaseDAO<City, Long> {
	
	
	/**
	 * Inserts a row in the City table.
	 * Do not need to pass the id, it will be generated.
	 * @param cityName 
	 * @return id generated by the database
	 */
	public Long saveCity(String cityName)
	{
		Session session = getSession();
	
		Long cityId = null;
		
			
			City city = new City();
			city.setName(cityName);
			cityId = (Long) session.save(city);
			
		
		return cityId;
	}
	
	/**
	 * List all the cities from the database
	 */
	@SuppressWarnings("unchecked")
	public void listCities()
	{
		Session session = getSession();
	
			List<City> cities = session.createQuery("from City").list();
			
			for (City city : cities){
				System.out.println(city.getName());
			}
			
		
	}
	
	/**
	 * Upate a city in the database
	 * @param cityId id of the city to be updated
	 * @param cityName name of the "new" city
	 */
	public void updateCity(Long cityId, String cityName)
	{
		Session session = getSession();
		
			City city = (City) session.get(City.class, cityId);
			city.setName(cityName);
			System.out.println(1);
		
	}
	
	/**
	 * Delete a city from database
	 * @param cityId id of the city to be deleted
	 */
	public void deleteCity(Long cityId)
	{
		Session session = getSession();
		
			City city = (City) session.get(City.class, cityId);
			session.delete(city);
		
	}
}
